package com.shop.loan.controller;


import com.shop.loan.annotation.LoginAuth;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.Constant;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.vo.CheckOrderRes;
import com.shop.loan.model.vo.LoanOrderVo;
import com.shop.loan.model.vo.OrderVo;
import com.shop.loan.service.AsyncService;
import com.shop.loan.service.LoanOrderService;
import com.shop.loan.service.MachineRuleService;
import io.swagger.annotations.*;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@Api(value = "订单管理", tags = "订单管理")
@RestController
@RequestMapping(Constant.appPrefex + "/order")
public class OrderController {

    @Resource
    LoanOrderService loanOrderService;

    @Lazy
    @Resource
    AsyncService asyncService;

    @Resource
    MachineRuleService machineRuleService;

    @LoginAuth
    @ApiOperation("订单确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsSizeId", value = "商品规格id", dataType = "string", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1, message = "成功", response = CheckOrderRes.class)
    })
    @PostMapping("/check")
    public ResResult check(@ApiIgnore LoanOrderVo loanOrderVo) {
        if (loanOrderVo.getGoodsSizeId() == null) {
            return new ResResult(Code.PARAM_LACK);
        }
        return new ResResult(Code.SUCCESS, loanOrderService.checkOrder(loanOrderVo));
    }

    @LoginAuth
    @ApiOperation("创建订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", dataType = "string", required = true),
            @ApiImplicitParam(name = "goodsSizeId", value = "商品规格id", dataType = "string", required = true),
            @ApiImplicitParam(name = "address", value = "收件地址", dataType = "string", required = true),
            @ApiImplicitParam(name = "mobile", value = "收件人手机号码", dataType = "string", required = true),
            @ApiImplicitParam(name = "name", value = "收件人名称", dataType = "string", required = true),
            @ApiImplicitParam(name = "buyRemark", value = "买家备注", dataType = "string"),
            @ApiImplicitParam(name = "location", value = "定位地址信息", dataType = "string", required = true),
    })
    @PostMapping("/create")
    public ResResult create(@ApiIgnore LoanOrderVo loanOrderVo) {
        return loanOrderService.create(loanOrderVo, false);
    }

    @LoginAuth
    @ApiOperation("订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchStatus", value = "订单状态 " +
                    "0、待付款" +
                    "1、平台待审核" +
                    "2、平台审核拒绝" +
                    "3、商户待审核" +
                    "4、商户审核拒绝" +
                    "6、待发货" +
                    "7、待收货" +
                    "8、待还款" +
                    "9、已结束" +
                    "10、待归还", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "string", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "string", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 1, message = "成功", response = LoanOrderByMybatis.class)
    })
    @PostMapping("/list")
    public ResResult list(@ApiIgnore LoanOrderVo loanOrderVo) {
        return loanOrderService.list(loanOrderVo);
    }

    @LoginAuth
    @ApiOperation("订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", required = true),
    })
    @PostMapping("/detail")
    public ResResult orderDetail(@ApiIgnore LoanOrderVo loanOrderVo) {
        return loanOrderService.detail(loanOrderVo);
    }

    @LoginAuth
    @ApiOperation("查看物流")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "string", required = true),
    })
    @PostMapping("/logistics/get")
    public ResResult detail(@ApiIgnore OrderVo orderVo) {
//        return logisticsService.get(orderVo);
        return new ResResult(Code.CODE_ERROR);
    }


//    @PostMapping("/logistics/runTask")
//    public ResResult runTask() {
//        logisticsService.updateLogisticsByIng();
//        return new ResResult(Code.SUCCESS);
//    }
//
    @PostMapping("/overdue/runTask")
    public ResResult runTaskByOverdue() {
        loanOrderService.overdueTask();
        return new ResResult(Code.SUCCESS);
    }
//
//    @PostMapping("/logistics/signature")
//    public ResResult signature(OrderVo orderVo) throws Exception {
//        orderUtil.logisticsSignature(orderVo.getOrderId());
//        return new ResResult(Code.SUCCESS);
//    }
//
//
    @PostMapping("/machine/createUpdateMachineReport")
    public ResResult createUpdateMachineReport(LoanOrderVo loanOrderVo) {
        machineRuleService.createUpdateMachineReport(loanOrderVo.getUserId());
        return new ResResult(Code.SUCCESS);
    }
//
//
//    @PostMapping("/contract/update")
//    public ResResult contractUpdate(OrderVo orderVo) {
//        asyncService.updateOrderContractAndLocation(orderVo.getOrderId(), orderVo.getLocation());
//        return new ResResult(Code.SUCCESS);
//    }

}
